import error from './error.js'

// 静态方法类
class LayaPromise {
  constructor () {}

  // 加载场景
  static loadScene (url) {
    return new Promise((resolve, reject) => {
      Laya.Scene.load(url, Laya.Handler.create(null, (res) => {
        if (!res) {
          reject(error.error('SCENE_LOAD_FAILED', '场景加载失败', url))
        }
        else {
          resolve(res)
        }
      }))
    })
  }

  // 打开场景
  static openScene (url, closeOthers = true, params = null) {
    return new Promise((resolve, reject) => {
      Laya.Scene.open(url, closeOthers, params, Laya.Handler.create(null, (res) => {
        if (!res) {
          reject(error.error('SCENE_OPEN_FAILED', '场景打开失败', url))
        }
        else {
          resolve(res)
        }
      }))
    })
  }

  // 读取JSON文件
  static loadJSON (url) {
    return new Promise((resolve, reject) => {
      Laya.loader.load(url, Laya.Handler.create(null, (res) => {
        if (!res) {
          reject(error.error('RESOURCE_LOAD_FAILED', '资源加载失败', url))
        }
        else {
          resolve(res)
        }
      }))
    })
  }
}

export default LayaPromise